package com.cx.service.wjj.Impl;

import com.cx.mapper.PageMapper;
import com.cx.mapper.ReceiveAddressMapper;
import com.cx.mapper.SendAddressMapper;
import com.cx.pojo.wjr.UserPage;
import com.cx.service.wjj.AdminPageService;
import com.cx.vo.AdminPageDetailInfo;
import com.cx.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminPageServiceImpl implements AdminPageService {
    @Autowired
    PageMapper pageMapper;
    @Autowired
    SendAddressMapper sendMapper;
    @Autowired
    ReceiveAddressMapper receiveMapper;
    @Transactional
    public ResponseResult addPage(UserPage userPage) {
        ResponseResult responseResult = new ResponseResult();

        int count1 = pageMapper.insertUserPageSend(userPage);
        int count2 = pageMapper.insertUserPageReceive(userPage);
        int count3 = pageMapper.insertUserPagePage(userPage);
        int affectedRows=count1+ count2+ count3;
        if(affectedRows>2){
            responseResult.setCode(1);
            responseResult.setMessage("添加成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return responseResult;
    }
    @Transactional()
    @Override
    public ResponseResult deletePage(Integer pageNum) {
        ResponseResult responseResult = new ResponseResult();
            int count1 = receiveMapper.deleteByPageNum(pageNum);
            int count2 = sendMapper.deleteByPageNum(pageNum);
            int count3 = pageMapper.deleteByPageNum(pageNum);
            int affectedRows = count1 + count2 + count3;

        if(affectedRows>2){
            responseResult.setCode(1);
            responseResult.setMessage("删除成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return responseResult;
    }
  @Transactional
    @Override
    public ResponseResult updatePage(UserPage userPage) {
        ResponseResult responseResult = new ResponseResult();
        int count1=sendMapper.updateSendPageSelective(userPage);
        int count2=receiveMapper.updateReceivePageSelective(userPage);
        int count3=pageMapper.updatePageSelective(userPage);
        int affectedRows=count1+ count2+ count3;
        if(affectedRows>2){
            responseResult.setCode(1);
            responseResult.setMessage("修改成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("修改失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectAll() {
        ResponseResult responseResult = new ResponseResult();
        List<AdminPageDetailInfo> pageList=pageMapper.selectAll();
        if(pageList!=null&&pageList.size()>0){
            responseResult.setCode(1);
            responseResult.setMessage("查询成功");
            responseResult.setData(pageList);
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("查询失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectByPageNum(Integer pageNum) {
        ResponseResult responseResult = new ResponseResult();
        List<AdminPageDetailInfo> adminPageDetailInfos =pageMapper.selectByPageNum(pageNum);
        if(null!=adminPageDetailInfos && adminPageDetailInfos.size()>0){
            responseResult.setCode(1);
            responseResult.setMessage("查询成功");
            responseResult.setData(adminPageDetailInfos);
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("查询失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectByName(String name) {
        ResponseResult responseResult = new ResponseResult();
        List<AdminPageDetailInfo> adminPageDetailInfos =pageMapper.selectByName(name);
        if(adminPageDetailInfos !=null&& adminPageDetailInfos.size()>0){
            responseResult.setCode(1);
            responseResult.setMessage("查询成功");
            responseResult.setData(adminPageDetailInfos);
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("查询失败");
        }
        return responseResult;

    }

    @Override
    public ResponseResult selectByPhone(String phone) {
        ResponseResult responseResult = new ResponseResult();
        List<AdminPageDetailInfo> adminPageDetailInfos =pageMapper.selectByPhone(phone);
        if(adminPageDetailInfos !=null&& adminPageDetailInfos.size()>0){
            responseResult.setCode(1);
            responseResult.setMessage("查询成功");
            responseResult.setData(adminPageDetailInfos);
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("查询失败");
        }
        System.out.println(responseResult);
        return responseResult;
    }

//    @Override
//    public ResponseResult selectByMulti(UserPage userPage) {
//
//        ResponseResult responseResult = new ResponseResult();
//        List<UserPage>  userPageList= pageMapper.selectByMulti(userPage);
//        System.out.println(userPageList);
//        if(null!=userPageList&&userPageList.size()>0){
//            responseResult.setCode(1);
//            responseResult.setMessage("查询成功");
//            responseResult.setData(userPageList);
//        }else {
//            responseResult.setCode(0);
//            responseResult.setMessage("查询失败");
//        }
//
//        return responseResult;
//    }
    @Override
    public ResponseResult selectByAny(UserPage userPage) {

        ResponseResult responseResult = new ResponseResult();
       List<UserPage>  userPageList= pageMapper.selectByAny(userPage);
        System.out.println(userPageList);
       if(null!=userPageList&&userPageList.size()>0){
           responseResult.setCode(1);
           responseResult.setMessage("查询成功");
           responseResult.setData(userPageList);
       }else {
           responseResult.setCode(0);
           responseResult.setMessage("查询失败");
       }

        return responseResult;
    }

}


